package cn.mingyuan.hadoop.fileoperation;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;

public class ReadeFileUseURL {
	static {
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
	}
	private String file;

	/**
	 * 
	 * @param filePath
	 *            文件路径
	 */
	public ReadeFileUseURL(String filePath) {
		this.file = filePath;
	}

	public InputStream getInputStream() throws MalformedURLException, IOException {
		return new URL(file).openStream();
	}

	public void printContent() {
		InputStream ins = null;
		try {
			ins = getInputStream();
			IOUtils.copyBytes(ins, System.out, 4096, true);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(ins);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ReadeFileUseURL reader = new ReadeFileUseURL("hdfs://localhost:9000/data/NCDC.txt");
		reader.printContent();
	}

}
